import os
import random
import math
import time
import creatorMain
import cv2 as cv
import numpy as np

width = 1024
height = 1280


def point_to_center_dis(point, center_):
    return math.sqrt(math.pow((point[0] - center_[0]), 2) + math.pow((point[1] - center_[1]), 2))


def add_salut_noisy(res, center, xmin, xmax, ymin, ymax, max_radius, min_radius):
    for ii in range(xmin, xmax):
        for jj in range(ymin, ymax):
            point_ = [ii, jj]
            if point_to_center_dis(point_, center) <= max_radius + 5:
                if point_to_center_dis(point_, center) >= min_radius - 5:
                    if np.random.rand() < 0.2:
                        if np.random.rand() < 0.5:
                            res[jj, ii] = random.randint(0, 100)
                        else:
                            res[jj, ii] = random.randint(130, 255)


def creator_img(file_path, res_path):
    random_img = creatorMain.create_img_random(width, height)
    color = 255
    img_ = 255 * np.zeros((width, height), dtype=np.uint8)
    img_2 = 255 * np.zeros((width, height), dtype=np.uint8)
    img_[:] = color
    img_2[:] = color
    max_radius = random.randint(145, 160)
    min_radius = max_radius - random.randint(20, 30)
    x = random.randint(max_radius, width - 2 * max_radius)
    y = random.randint(max_radius, height - 2 * max_radius)
    center = [y, x]
    xmin = center[0] - max_radius
    ymin = center[1] - max_radius
    xmax = center[0] + max_radius
    ymax = center[1] + max_radius

    centerx = center[0] / height
    centery = center[1] / width
    dx = (2 * min_radius) / height
    dy = (2 * min_radius) / width

    dx2 = (2 * max_radius) / height
    dy2 = (2 * max_radius) / width

    res_path.write("0" + ' ')
    res_path.write(str(centerx) + ' ')
    res_path.write(str(centery) + ' ')
    res_path.write(str(dx) + ' ')
    res_path.write(str(dy) + ' ' + '\n')

    res_path.write("1" + ' ')
    res_path.write(str(centerx) + ' ')
    res_path.write(str(centery) + ' ')
    res_path.write(str(dx2) + ' ')
    res_path.write(str(dy2) + ' ' + '\n')

    blcakcolor = random.randint(0, 100)
    cv.circle(img_, (center[0], center[1]), max_radius, blcakcolor, 0, cv.LINE_AA)
    cv.circle(img_2, (center[0], center[1]), min_radius, blcakcolor, 0, cv.LINE_AA)

    difference = cv.absdiff(img_, img_2)
    _, threshold = cv.threshold(difference, 20, 255, cv.THRESH_BINARY)

    for ii in range(xmin, xmax):
        for jj in range(ymin, ymax):
            point_ = [ii, jj]
            if point_to_center_dis(point_, center) <= max_radius:
                if point_to_center_dis(point_, center) >= min_radius:
                    threshold[jj, ii] = 230
    res = cv.add(threshold, random_img)
    # cv.imshow("i", res)
    # cv.waitKey(0)
    add_salut_noisy(res, center, xmin, xmax, ymin, ymax, max_radius, min_radius)
    # 中值
    # blurred = creatorMain.add_median_filter(res, kernel_size=3)
    # 均值
    blurred = creatorMain.add_mean_filter(res, kernel_size=3)
    # 高斯
    ksize = (5, 5)
    sigma = 0
    blurred2 = cv.GaussianBlur(blurred, ksize, sigma)
    # cv.rectangle(blurred2, (xmin, ymin), (xmax, ymax), 255, 5)
    # cv.imwrite(file_path, blurred2)

    cv.imshow("omg", blurred2)
    cv.waitKey(0)
    # cv.imshow("omg1", blurred2)
    # cv.waitKey(0)
    # cv.imshow("res", random_img)
    # cv.waitKey(0)


if __name__ == '__main__':
    root_dir = "/home/mikoptik/yolov7_test/yolo_v7/match_circle"
    dir_num = 1
    img_num = 2
    for i in range(1, dir_num + 1):
        folder_path = os.path.join(root_dir, str(i))
        res_path_d = os.path.join(folder_path, "res")
        txt_path = os.path.join(folder_path, str(i) + '.txt')
        print(i, " begin")
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        if not os.path.exists(res_path_d):
            os.makedirs(res_path_d)
        file = open(txt_path, 'w')
        for j in range(1, img_num + 1):
            start_time = time.time()
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            file_path = os.path.join(folder_path, "ring" + str(j) + '.bmp')
            res_path = os.path.join(res_path_d, "ring" + str(j) + ".txt")
            res_file = open(res_path, 'w')
            creator_img(file_path, res_file)
            end_time = time.time()
            print(j, "-->finish", "time-->", end_time - start_time)
